package org.akomantoso.core.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.InputConstants;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * The complex type listItems specifies the content model of elements ul and ol, and specifies just a sequence of list
 * items (elements li).
 * 
 * <pre>
 * <xsd:complexType name="listItems">
 * 		<xsd:sequence>
 * 			<xsd:element ref="li" minOccurs="1" maxOccurs="unbounded"/>
 * 		</xsd:sequence>
 * 		<xsd:attributeGroup ref="corereq"/>
 * </xsd:complexType>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 */
public abstract class ListItems extends CoreReqImpl {

	private final List<Li> _lis = new ArrayList<Li>(4);

	public List<Li> getLis() {
		return _lis;
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	public <T extends Output> void writeExternal(T out) throws IOException {
		super.writeExternal(out);
		for (Li li : _lis) {
			li.writeExternal(out);
		}
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	public <T extends Input> void readExternal(T in) throws IOException {
		super.readExternal(in);
		final String localName = in.getLocalName();
		Li li = null;
		while (true) {
			in.readNext();
			switch (in.getEventType()) {
			case InputConstants.START_ELEMENT:
				_lis.add(li = new Li());
				li.readExternal(in);
				break;
			case InputConstants.END_ELEMENT:
				if (localName.equals(in.getLocalName())) {
					return;
				}
			}
		}
	}

	protected final void internalAccept(AKVisitor visitor) {
		for (Li li : _lis) {
			li.accept(visitor);
		}
	}
}